﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using bus.console.service.bus.vo;
using System.Web.Script.Serialization;
using web.ui.Service_Member;
using NHibernate.Criterion;

public partial class Service_Member : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) 
        {
            doAction();
        }
    }

    private void doAction()
    {
        String id = Request.QueryString["id"];
        String sou_type = Request.QueryString["sou_type"];
        String sou_id = Request.QueryString["sou_id"];

        if (!String.IsNullOrEmpty(id))
        {
            Member m = busService.myService.DaoGetVOById<Member>(id);
            MemberProfile profile = busService.myService.DaoGetVOById<MemberProfile>(id);

            if (m != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                ResultShowVO vo = new ResultShowVO();
                vo.member = toShowMember(m);
                vo.profile = toShowProfile(profile);

                String json = serializer.Serialize(vo);
                Response.Write(json);
                Response.End();
            }
            else             
            {
                emptyResult();
            }
        }
        else if (!String.IsNullOrEmpty(sou_type) && !String.IsNullOrEmpty(sou_id))
        {
            DetachedCriteria dCriteria = DetachedCriteria.For<Member>();
            dCriteria.Add(Expression.Eq("SourceType", sou_type));
            dCriteria.Add(Expression.Eq("SourceID", sou_id));

            IList<Member> mList = busService.myService.ExecutableDetachedCriteria<Member>(dCriteria, 0, 1);

            if (mList.Count == 1)
            {
                Member m = mList[0];
                MemberProfile profile = busService.myService.DaoGetVOById<MemberProfile>(m.ID);
                JavaScriptSerializer serializer = new JavaScriptSerializer();

                ResultShowVO vo = new ResultShowVO();
                vo.member = toShowMember(m);
                vo.profile = toShowProfile(profile);

                String json = serializer.Serialize(vo);
                Response.Write(json);
                Response.End();
            }
            else 
            {
                emptyResult();
            }
        }
        else
        {
            emptyResult();
        }
    }

    private void emptyResult()
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();

        ResultShowVO vo = new ResultShowVO();
     

        String json = serializer.Serialize(vo);
        Response.Write(json);
        Response.End();
    }

    private MemberProfileShowVO toShowProfile(MemberProfile profile)
    {
        MemberProfileShowVO s = new MemberProfileShowVO();
        s.point = profile.JoyPoint;
        s.max_notice = profile.MaxNoticeNum;
        s.push = profile.PushNotice;
        return s;
    }

    private MemberShowVO toShowMember(Member m)
    {
        MemberShowVO s = new MemberShowVO();
        s.id = m.ID;
        s.dev_token = m.DeviceToken;
        s.sou_id = m.SourceID;
        s.sou_type = m.SourceType;
        return s;
    }
}
namespace web.ui.Service_Member 
{
    class ResultShowVO 
    {
       public virtual  MemberShowVO member { get; set; }
       public virtual  MemberProfileShowVO profile { get; set; }

    }

    class MemberShowVO 
    {
        public String id  {get;set;}

        
        public virtual string sou_type { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public virtual string sou_id { get; set; }


        public virtual string dev_token { get; set; }
    }

    class MemberProfileShowVO
    {
        public virtual int push { get; set; }

        /// <summary>
        /// 每天最多接收幾則推播0:無限制
        /// </summary>
        public virtual int max_notice { get; set; }

        /// <summary>
        /// 樂點幣
        /// </summary>
        public virtual int point { get; set; }
    }
}